package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;

import com.rjwh.dingdong.client.R;

/**
 * 带圆点指引的viewpager
 */
public class IndicatorHackyViewPager extends HackyViewPager {

    private Context context;
    private Paint paint;

    public IndicatorHackyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        paint = new Paint();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawCycle(canvas);
    }

    private void drawCycle(Canvas canvas) {
        canvas.save();
        canvas.translate(getScrollX(), getScrollY());
        int count = 0;
        if (this.getAdapter() != null) {
            count = this.getAdapter().getCount();
        }
        int select = getCurrentItem();
        float density = getContext().getResources().getDisplayMetrics().density;
        int itemWidth = (int) (14 * density);
        int itemHeight = itemWidth / 2;
        int x = (getWidth() - count * itemWidth) / 2;
        int y = getHeight() - itemWidth;
        int minItemHeight = (int) ((float) itemHeight * 0.8F);
        paint.setAntiAlias(true);

        paint.setStyle(Style.FILL);
        for (int i = 0; i < count; i++) {
            //数据源集合只有一个item时不显示圆点
            if (count == 1) {
                return;
            }
            if (select == i) {
                paint.setColor(context.getResources().getColor(R.color.white));
                /**
                 * cx：圆心的x坐标;
                 * cy：圆心的y坐标;
                 * radius：圆的半径;
                 * paint：绘制时所使用的画笔。
                 */
                canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y + minItemHeight, minItemHeight / 2, paint);
            } else {
                paint.setColor(context.getResources().getColor(R.color.black_alpha_30));
                canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y + minItemHeight, minItemHeight / 2, paint);
            }
        }
        canvas.restore();
    }

}